package lol.clann.bukkit.pluginboot.newcommand;

import lol.clann.clannboot.command.CommandTree;
import lol.clann.clannboot.command.exception.CommandParamParseFailException;
import lol.clann.clannboot.exception.NestedException;
import org.bukkit.command.Command;
import org.bukkit.command.CommandException;
import org.bukkit.command.CommandSender;

import java.util.List;

/**
 * 用来对接Bukkit
 * 将bukkit指令转发到CommandTree
 *
 * @author pyz
 * @date 2018/11/27 9:31 PM
 */
public class BukkitCommandAdapter extends Command {

    private CommandTree command;

    public BukkitCommandAdapter(CommandTree command) {
        super(command.getName(), command.getDesc(), "", command.getAlias());
        this.command = command;
    }

    @Override
    public boolean execute(CommandSender commandSender, String s, String[] args) {
        try {
            return command.execute(new BukkitCommandSender(commandSender), args);
        } catch (NestedException e) {
            throw new CommandException(e.getMessage(), e.getNestedException());
        } catch (CommandParamParseFailException e) {
            throw new CommandException(e.getMessage(), e);
        }
    }

    @Override
    public List<String> tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException {
        return command.suggest(new BukkitCommandSender(sender), args, 0);
    }
}
